<?
    require_once("config.php");
    
    $menu = new Menu("FAQ Category Administration");
    $menu->AddItem(new MenuItem("Back to Question Index","faq.php?action=list",AuthConstants::ADMINISTRATORS));
    $menu->AddItem(new MenuItem("Category List","faqcategories.php",AuthConstants::ADMINISTRATORS));
    $menu->AddItem(new MenuItem("Add Category","faqcategories.php?action=edit",AuthConstants::ADMINISTRATORS));
    
    if ($_POST) {
        $array = array('categoryname' => $_POST['categoryname']);
        $query = '';
        if ($_POST['id']) $query = QueryTools::CreateQuery($array,"faq_categories","update",$_POST['id']);
        else $query = QueryTools::CreateQuery($array,"faq_categories");
        
        DBM::Execute($query);
        header("Location: faqcategories.php");
    }
    else if ($_GET['action'] == "edit") {
        $action = ($_GET['id'] ? "Edit" : "Add");
        MenuManager::AddMenu("$action FAQ Category",$menu);
        Functions::PrintHeader("$action FAQ Category");
        ?>
        <form method="POST" action="faqcategories.php">
            <?
                $info = array();
                if ($_GET['id']) {
                    $info = DBM::FetchRow("SELECT * FROM faq_categories WHERE id=$_GET[id];");
                    echo "<input type='hidden' name='id' value='$_GET[id]' />";
                }
            ?>
            <table class='cgltable'>
                <tr>
                    <th colspan='2'><?=$action?> FAQ Category</th>
                </tr>
                <? if ($_GET['id']) { ?>
                <tr>
                    <td class='label'>Category ID</td>
                    <td><?=$info['id']?></td>
                </tr>
                <? } ?>
                <tr>
                    <td class='label'>Category Name</td>
                    <td><input type='text' size='35' name='categoryname' value='<?=$info['categoryname']?>' /></td>
                </tr>
                <tr>
                    <td colspan='2'><input type='reset' value='Start Over' /><input type='submit' value='Save Category Name' /></td>
                </tr>
            </table>
        </form>
        <?
        Functions::PrintFooter();
    }
    else if ($_GET['action'] == "delete") {
    }
    else {
        MenuManager::AddMenu("Category List",$menu);
        Functions::PrintHeader("Category List");

        $categories = DBM::FetchAllRows("SELECT * FROM faq_categories ORDER BY categoryname ASC;");
        echo "<table class='cgltable' align='center'>";
        echo "<tr><th>Category</th><th style='width:150px;'>&nbsp;</th></tr>";
        foreach ($categories as $category) {
            echo "<tr>
                    <td>$category[categoryname]</td>
                    <td>
                        <a href='faqcategories.php?action=edit&id=$category[id]'>edit</a> | 
                        <a href='faqcategories.php?action=delete&id=$category[id]'>delete</a>
                    </td>
                  </tr>";
        }
        echo "</table>";
        
        Functions::PrintFooter();
    }
?>